# coding: utf-8

try:
    import ConfigParser
    conf = ConfigParser.ConfigParser()
except ImportError:
    import configparser
    conf = configparser.ConfigParser()

conf.read("test.conf")

# get the specified section and option
name = conf.get("section1", "name")
print(name)

age = conf.get("section1", "age")
print(age)

# get all sections
sections = conf.sections()
print(sections)

# write the conf file
# update the specified section and option
conf.set("section2", "port", "8001")

# add option
conf.set("section2", "tcp_port", "80")

# add section
conf.add_section("my_section")
conf.set("my_section", "my_option", "http://lavenliu.blog.51cto.com")

# write back to conf file
conf.write(open("test.conf", "w"))
